package com.van.bo.badwrting.edit.process;


import com.fasterxml.jackson.core.JsonProcessingException;
import com.van.bo.badwrting.edit.domain.JavaDataInES;
import com.van.bo.badwrting.edit.domain.ParamData;
import com.van.bo.badwrting.edit.service.DataService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

@Controller
public class AddDataController {


    @Resource
    private DataService dataService;

    @PostMapping("/add/save")
    public ModelAndView saveData(@Param("message") JavaDataInES javaDataInES) {

        if (StringUtils.isNotBlank(javaDataInES.getId())) {
            javaDataInES.setUpdateTime(new Date());
        }
        dataService.saveData(javaDataInES);
        return findJavaDataList(new ParamData());
    }

    @GetMapping("/add/delete")
    public ModelAndView delete(@Param("message") JavaDataInES javaDataInES) {
        dataService.deleteById(javaDataInES);
        return findJavaDataList(new ParamData());
    }


    //thymeleaf form表单的th:action链接一定要加/
    @GetMapping("/add/edit")
    public ModelAndView edit(ParamData pd) {
        ModelAndView mv = new ModelAndView();
        mv.setViewName("edit");
        if (StringUtils.isBlank(pd.getId())) {
            mv.addObject("data", new JavaDataInES());
            return mv;
        }
        JavaDataInES data = dataService.findById(pd.getId());
        mv.addObject("data", data);
        return mv;
    }

    //thymeleaf form表单的th:action链接一定要加/
    @GetMapping("/list")
    public ModelAndView findJavaDataList(ParamData pd) {

        List<JavaDataInES> dataList = dataService.findJavaDataList(pd);
        ModelAndView mv = new ModelAndView();
        mv.addObject("dataList", dataList);
        mv.addObject("search", new ParamData());
        mv.addObject("lastSearch", pd);
        mv.setViewName("list");


        return mv;
    }

    public static void main(String[] args) {
        String s1 = "Programming";
        String s2 = new String("Programming");
        String s3 = "Program";
        String s4 = "ming";
        String s5 = "Program" + "ming";
        String s6 = s3 + s4;
        System.out.println(s1 == s2);
        System.out.println(s1 == s5);
        System.out.println(s1 == s6);
    }


    //thymeleaf form表单的th:action链接一定要加/
    @PostMapping("/add/upload")
    public ModelAndView upload(@RequestParam("file") MultipartFile file, JavaDataInES jdies) throws JsonProcessingException {
        ModelAndView mv = new ModelAndView();
        mv.addObject("jdies", jdies);
        mv.addObject("search", new ParamData());
        mv.setViewName("file_upload");
        return mv;
    }

    //thymeleaf form表单的th:action链接一定要加/
    @GetMapping("/up")
    public ModelAndView up(ParamData pd) {

        ModelAndView mv = new ModelAndView();
        JavaDataInES jdies = new JavaDataInES();
        jdies.setData("hello");
        mv.addObject("jdies", jdies);
        mv.addObject("lastSearch", pd);
        mv.setViewName("file_upload");
        return mv;
    }


}
